<?php
$id = abs(intval($_GET['id']));

$team = Table::Fetch('team', $id);
//if ( !$team || $team['begin_time']>time() ) {
//	Session::Set('error', '优惠项目不存在');
////	redirect( WEB_ROOT . '/index.php' );
//}

$ex_con = array(
		'user_id' => $login_user_id,
		'team_id' => $team['id'],
		'state' => 'unpay',
		);
$order = DB::LimitQuery('order', array(
	'condition' => $ex_con,
	'one' => true,
));

/* buyonce */
//if (strtoupper($team['buyonce'])=='Y') {
//	$ex_con['state'] = 'pay';
//	if ( Table::Count('order', $ex_con) ) {
//		Session::Set('error', '您已经成功购买了本单产品，请勿重复购买，快去关注一下其他产品吧！');
////		redirect( WEB_ROOT . "/team.php?id={$id}"); 
//	}
//}
//if (strtoupper($team['buyonce'])=='Y') {
//	$ex_con['state'] = 'sync';
//	if ( Table::Count('order', $ex_con) ) {
//		Session::Set('error', '抱歉，商家规定本活动只能参加一次，快去关注一下其他活动吧！');
////		redirect( WEB_ROOT . "/team.php?id={$id}"); 
//	}
//}
/* End */

//peruser buy count
//if ($team['per_number']>0) {
//	$now_count = Table::Count('order', array(
//		'user_id' => $login_user_id,
//		'team_id' => $id,
//		'state' => 'pay',
//	), 'quantity');
//	$team['per_number'] -= $now_count;
//	if ($team['per_number']<=0) {
//		Session::Set('error', '您购买本单产品的数量已经达到上限，快去关注一下其他产品吧！');
////		redirect( WEB_ROOT . "/team.php?id={$id}"); 
//	}
//}

	
$table = new Table('order', $_POST);
	
// check quantity
$table->quantity = 1;
//$table->quantity = abs(intval($table->quantity));
//if ( $table->quantity == 0 ) {
//	Session::Set('error', '购买数量不能小于1份');
//	redirect( WEB_ROOT . "/team/buy.php?id={$team['id']}");
//} 
//elseif ( $team['per_number']>0 && $table->quantity > $team['per_number'] ) {
//	Session::Set('error', '您本次购买本单产品已超出限额！');
//	redirect( WEB_ROOT . "/team.php?id={$id}"); 
//}


/* check mobile */
if ( ! Utility::IsMobile($_POST['mobile']) ) {
	Session::Set('error', '手机号码有误！');
	redirect( WEB_ROOT . "/team/buy.php?id={$team['id']}");
} elseif($_POST['mobile'] != $login_user['mobile']){
	$update = array(
		'mobile' => $_POST['mobile'],
	);
	if ( ! ZUser::Modify($login_user['id'], $update) ) {
		Session::Set('error', '修改账户设置中的手机号失败');
		redirect( WEB_ROOT . '/account/settings.php ');
	} else {
		Session::Set('notice', '同步修改了您账户设置中的手机号');
	}
}
/* End */

// 已生成过订单，但还没有完成发布消息操作，此次为继续上次操作（pay代表同步消息操作）
if ($order && $order['state']=='unpay') {
	$table->id = $order['id'];
}

$table->user_id = $login_user_id;
$table->team_id = $team['id'];
$table->city_id = $team['city_id'];
$table->express = ($team['delivery']=='express') ? 'Y' : 'N';
$table->fare = $table->express=='Y' ? $team['fare'] : 0;
$table->price = $team['team_price'];
$table->credit = 0;

if ( $table->id ) {
	$eorder = Table::Fetch('order', $table->id);
	if ($eorder['state']=='unpay' 
			&& $eorder['team_id'] == $id
			&& $eorder['user_id'] == $login_user_id
	   ) {
		$table->origin = ($table->quantity * $team['team_price']) + ($team['delivery'] == 'express' ? $team['fare'] : 0) - $eorder['card'];
	} else {
		$eorder = null;
	}
}

if (!$eorder) {
	$table->create_time = time();
	$table->origin = ($table->quantity * $team['team_price']) + ($team['delivery'] == 'express' ? $team['fare'] : 0);
}

$insert = array(
		'user_id', 'team_id', 'city_id', 'state', 
		'fare', 'express', 'origin', 'price',
		'address', 'zipcode', 'realname', 'mobile', 'quantity',
		'create_time', 'remark',
		);

// 确认后，同步消息
if ($flag = $table->update($insert)) {
	$order_id = abs(intval($table->id));
	
	// 适配
	$_GET['id'] = $order_id;
	require_once(dirname(__FILE__) . '/sync.php');
	
//	redirect(WEB_ROOT."/connect/sync.php?id={$order_id}");
}